package com.mlethe.android.demo.recyclerview.adapter.adapter;


import androidx.annotation.NonNull;

import com.mlethe.android.demo.recyclerview.adapter.entity.Dog;
import com.mlethe.android.demo.recyclerview.adapter.entity.Level0Item;
import com.mlethe.android.demo.recyclerview.adapter.entity.Level1Item;
import com.mlethe.android.demo.recyclerview.adapter.entity.Level2Item;
import com.mlethe.demo.recyclerview.adapter.R;
import com.mlethe.library.recyclerview.adapter.BaseQuickAdapter;
import com.mlethe.library.recyclerview.entity.AbstractExpandableItem;
import com.mlethe.library.recyclerview.listener.OnItemClickListener;
import com.mlethe.library.recyclerview.listener.OnItemLongClickListener;
import com.mlethe.library.recyclerview.viewholder.ViewHolder;

/**
 * @author Mlethe
 * @date 2021/4/27
 */
public class ExpandableItemAdapter extends BaseQuickAdapter<Object> {

    private boolean isOnlyExpandOne = false;

    @Override
    protected int getLayoutId(Object item, int position) {
        if (item instanceof AbstractExpandableItem) {
            int level = ((AbstractExpandableItem) item).getLevel();
            if (level == 0) {
                return R.layout.item_expandable_lv0;
            } else if (level == 1) {
                return R.layout.item_expandable_lv1;
            } else if (level == 2) {
                return R.layout.item_expandable_lv1;
            }
        }
        return R.layout.item_expandable_lv2;
    }

    @Override
    protected void convert(@NonNull ViewHolder holder, Object item, int position) {
        if (item instanceof AbstractExpandableItem) {
            int level = ((AbstractExpandableItem) item).getLevel();
            if (level == 0) {
                switch (holder.getLayoutPosition() % 3) {
                    case 0:
                        holder.setImageResource(R.id.iv_head, R.mipmap.head_img0);
                        break;
                    case 1:
                        holder.setImageResource(R.id.iv_head, R.mipmap.head_img1);
                        break;
                    case 2:
                        holder.setImageResource(R.id.iv_head, R.mipmap.head_img2);
                        break;
                    default:
                        break;
                }
                final Level0Item lv0 = (Level0Item) item;
                holder.setText(R.id.title, lv0.title)
                        .setText(R.id.sub_title, lv0.subTitle)
                        .setImageResource(R.id.iv, lv0.isExpanded() ? R.mipmap.arrow_b : R.mipmap.arrow_r)
                        .setOnItemClickListener((OnItemClickListener<Level0Item>)(view, item1, position1) -> {
                            if (lv0.isExpanded()) {
                                collapse(position1);
                            } else if (isOnlyExpandOne) {
                                expandOnlyOne(position1);
                            } else {
                                expand(position1);
                            }
                        });
                return;
            } else if (level == 1) {
                final Level1Item lv1 = (Level1Item) item;
                holder.setText(R.id.title, lv1.title)
                        .setText(R.id.sub_title, lv1.subTitle)
                        .setImageResource(R.id.iv, lv1.isExpanded() ? R.mipmap.arrow_b : R.mipmap.arrow_r)
                        .setOnItemClickListener((OnItemClickListener<Level1Item>) (view, item1, position1) -> {
                            if (lv1.isExpanded()) {
                                collapse(position1, false);
                            } else {
                                expand(position1, false);
                            }
                        })
                        .setOnItemLongClickListener((OnItemLongClickListener<Level1Item>) (view, item1, position1) -> {
                            removeChild(position1);
                            return true;
                        });
                return;
            } else if (level == 2) {
                final Level2Item lv2 = (Level2Item) item;
                holder.setText(R.id.title, lv2.title)
                        .setText(R.id.sub_title, lv2.subTitle)
                        .setImageResource(R.id.iv, lv2.isExpanded() ? R.mipmap.arrow_b : R.mipmap.arrow_r)
                        .setOnItemClickListener((OnItemClickListener<Level2Item>) (view, item1, position1) -> {
                            if (lv2.isExpanded()) {
                                collapse(position1, false);
                            } else {
                                expand(position1, false);
                            }
                        })
                        .setOnItemLongClickListener((OnItemLongClickListener<Level2Item>) (view, item1, position1) -> {
                            removeChild(position1);
                            return true;
                        });
                return;
            }
        }
        final Dog dog = (Dog) item;
        holder.setText(R.id.tv, dog.name + " parent pos: " + getParentPosition(dog))
                .setOnItemClickListener((OnItemClickListener<Dog>) (view, item1, position1) -> {
                    removeChild(position1);
                });
    }

    public void setOnlyExpandOne(boolean onlyExpandOne) {
        isOnlyExpandOne = onlyExpandOne;
    }
}